import Mock from 'mockjs';
import U from '@/server/mock/users.json'
import books from '@/server/mock/books.json'
let users = U;

Mock.mock('/mock/getBooks','get',{
	code:200,
	data:books
})

// 登录接口
Mock.mock('/api/login', 'post', (req) => {
  const { username, password } = JSON.parse(req.body);
  const user = users.find(u => u.username === username && u.passwd === password);
  if (user) {
    return {
      code: 200,
      message: '登录成功',
      data: {
        token:username
      }
    };
  } else {
    return {
      code: 401,
      message: '用户名或密码错误'
    };
  }
});

// 注册接口
Mock.mock('/api/register', 'post', (req) => {
  const { username, password } = JSON.parse(req.body);
  const existingUser = users.find(u => u.username === username);
  const e = uu.find(u => u.username === username);
  if (existingUser) {
    return {
      code: 409,
      message: '用户名已存在'
    };
  }
  users.push({ username, password });
  return {
    code: 200,
    message: '注册成功'
  };
});
